import Vue from 'vue'
import VueRouter from 'vue-router'

import Publish from './view/publish'
import ArticleList from './view/articleList'
import Management from './view/management'
import Login from './view/login'

Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'history',
  routes: [
    {
      name: 'login',
      path: '/login',
      component: Login
    },
    {
      name: 'management',
      path: '/management',
      component: Management,
      beforeEnter: (to, from, next) => {
        if (sessionStorage.getItem('token')) {
          next()
        } else {
          Vue.prototype.$message({
            type: 'error',
            message: '登录身份过期，或未登录！'
          })
          router.push('/login')
        }
      },
      children: [
        {
          name: 'publish',
          path: 'publish',
          component: Publish
        },
        {
          name: 'articleList',
          path: 'articleList',
          component: ArticleList
        },
      ]
    },
  ]
})

export default router